const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
    },
    status: {
      type: Number,
      value: -1,
    },
    isBargain: {
      type: Number,
      value: false,
    },
    prizeInfo: {
      type: Object,
      value: {},
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    close() {
      this.triggerEvent('close')
    },
    bargain() {
      console.log(this.data.status)
      // let type = e.currentTarget.dataset.status;
      if (this.data.status == 0) {
        this.getSubscribeMessage()
      } else {
        this.triggerEvent('bargain')
      }
    },
    getSubscribeMessage: function () {
      let that = this;
      memberModel.subscribeMessage({
        merchantId: wx.getStorageSync('merchantId'),
        position: 6
      }).then(data => {
        if (data && data.length > 0) {
          let tmplId = []
          data.forEach(element => {
            if (element.templateId) {
              tmplId.push(element.templateId)
            }
          })
          if (tmplId.length > 0) {
            wx.requestSubscribeMessage({
              tmplIds: tmplId,
              success(res) {
                that.triggerEvent('bargain')
              },
              fail(err) {
                that.triggerEvent('bargain')
              }
            })
          }
        } else {
          that.triggerEvent('bargain')
        }
      }).catch(err => {
        that.triggerEvent('bargain')
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },
    participation() {
      this.triggerEvent('participation')
    }
  }
})